import java.io.*;

/**
 * A program that counts the number of words in any type of file or on the system's standard input.
 * The application allows the user to add it(s own delimiters with the -b command. Check the user doc for more specific 
 * information.
 * @author Mathieu Morel, Pradyut Pokuri, Emily Rice 
 * @version 1.0
 */
public class WordCount {

	/**
	 * The program main method, invoked when program is launched
	 * @param args the list of the arguments passed to the method:
	 * -b <delimiter> if the delimiter option is used
	 * filename if a file is passed
	 * @throws IOException
	 */
	public static void main(String[] args)
			throws IOException {
		
		//Regex of required delimiters string escaped
		String delims = "[\\,\\.\\:\\;\\s]+";
		
		//Class to process command line text input
		StandardInputWordCounter siwc = new StandardInputWordCounter();
		
		//Class to process file input
		FileWordCounter fwc = new FileWordCounter();
		
		if(args.length==0) //No arguments - Command line input is  "java WordCount"
			siwc.wordCount(delims);
		
		else if(args.length==1) 
			
			if(args[0]=="-b") // If only one argument and it is the -b param, new delims
							// not specified, so assume default command line text input 
				siwc.wordCount(delims);
			else   // Else argument is file to process
				fwc.wordCount(args[0], delims);
		
		else if(args.length==3) //If 3 arguments, it's file input with new delims
			
			if(args[0].equals("-b")) //User inputs -b [break chars] file 
				fwc.wordCount(args[2], "(["+args[1]+"]|"+delims+")+");
			else //User inputs file -b [break chars]
				fwc.wordCount(args[0], "(["+args[2]+"]|"+delims+")+");	
		
		else // Only other possibility is two arguments, command line input with new delims
			siwc.wordCount("(["+args[1]+"]|"+delims+")+");
	}
}